home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Apple II / System 6.0 Sample Code / FinderEx ƒ / FinderEx.c next >
Encoding:
C/C++ Source or Header  |  1993-06-15  |  6.6 KB  |  245 lines  |  [TEXT/MPS ]

  1. /********************************************************************************
  2.  *
  3.  * File:           FinderEx.c
  4.  * Version:        1.2
  5.  * Author:         David A. Lyons
  6.  * Copyright:      (c) 1991-92 by Apple Computer, Inc.
  7.  *
  8.  * Description:    Skeleton Finder 6.0 Extension in MPW IIgs C
  9.  *
  10.  *----------------------------------------------------------
  11.  *
  12.  * Development History:
  13.  *
  14.  * 25-Jun-91 DAL
  15.  *
  16.  * Initial version.
  17.  *
  18.  * 17-Oct-91 DAL
  19.  *
  20.  * Updated for new interface files, and to make it a little more interesting.
  21.  * (Happy Anniversary, Loma Prieta.)
  22.  *
  23.  * 24-Feb-92 DAL    Version 1.1
  24.  *
  25.  * Our tellFinder SendRequest calls now have the stopAfterOne bit set (there is
  26.  * only one Finder request procedure to receiv ethem, so it's slightly more
  27.  * efficient to stop right away instead of making SendRequest look for
  28.  * additional "Apple~Finder~..." procedures to send our request to).
  29.  *
  30.  * We now accept the srqGoAway code so we can be removed or replaced on the fly.
  31.  * (If someone wants to remove us while the Finder is active, they should send
  32.  * us a finderSaysGoodbye message first.)
  33.  *
  34.  * 25-Feb-92 DAL    Version 1.2
  35.  *
  36.  * Cleaned up our handling of finderSaysGoodbye.  Redundant requests should
  37.  * do no harm (for example, we shouldn't call tellFinderRemoveFromExtras with
  38.  * itemID zero).
  39.  *
  40.  ********************************************************************************/
  41.  
  42. #include <Types.h>
  43. #include <Quickdraw.h>
  44. #include <Memory.h>
  45. #include <IntMath.h>
  46. #include <Locator.h>
  47. #include <Window.h>
  48. #include <Menu.h>
  49. #include <Desk.h>
  50. #include <QDAux.h>
  51. #include <MiscTool.h>
  52.  
  53. /* #include <GSOS.h>      */
  54. /* #include <Font.h>      */
  55. /* #include <Event.h>     */
  56. /* #include <ProDOS.h>    */
  57. /* #include <Control.h>   */
  58. /* #include <List.h>      */
  59. /* #include <Scrap.h>     */
  60. /* #include <Dialog.h>    */
  61. /* #include <StdFile.h>   */
  62. /* #include <Print.h>     */
  63. /* #include <LineEdit.h>  */
  64. /* #include <Resources.h> */
  65.  
  66. #include <Finder.h>
  67.  
  68. int _toolErr;  /* normally defined in Start.obj */
  69.  
  70. char nameOfFinder[] = NAME_OF_FINDER;  /* this avoids multiple copies of string */
  71.  
  72. unsigned myItemID;
  73.  
  74. pascal unsigned myRequestProc();
  75. unsigned handleHello(),
  76.          handleGoodbye(),
  77.          handleExtrasChosen(),
  78.          handleGoAway();
  79. unsigned NumberOfIconsSelected();
  80.  
  81. /* The following must be the function executed when this Init is run */
  82.  
  83. void Installation()
  84. {
  85.     int oldB = SaveDB();
  86.  
  87.     /* Check for System 6.0 -- QuickDraw version must be >=3.7 */
  88.     if( (QDVersion() & 0x0FFF) < 0x0307)
  89.         SysBeep();
  90.     else
  91.       {
  92.         /* register our request procedure with the system */
  93.         AcceptRequests("\pSampleCompanyName~SampleProductName~",
  94.                          MMStartUp(),
  95.                          myRequestProc);
  96.       }
  97.  
  98.     RestoreDB(oldB);
  99. }
  100.  
  101.  
  102. /*
  103.   Here's the extension's request procedure.  The system calls this a lot, even
  104.   reentrantly--so be careful not to mess up any global variables that are needed
  105.   by other calls in progress.
  106.  
  107.   DO NOT USE LOTS OF STACK SPACE FOR REQUESTS YOU DO NOT ACCEPT!  Some requests
  108.   (like systemSaysBeep) can get sent while the stack is in page one, and if you
  109.   use too much the system will crash.
  110.  
  111. */
  112.  
  113. pascal unsigned myRequestProc(request, dataIn, dataOut)
  114.     unsigned request;
  115.     long dataIn;
  116.     long dataOut;
  117. {
  118.     unsigned oldB = SaveDB();
  119.     unsigned result = 0;
  120.  
  121.     switch(request)
  122.       {
  123.         case finderSaysHello:
  124.             result = handleHello();
  125.             break;
  126.  
  127.         case finderSaysGoodbye:
  128.             result = handleGoodbye();
  129.             break;
  130.  
  131.         case finderSaysExtrasChosen:
  132.             result = handleExtrasChosen( (unsigned) dataIn );
  133.             break;
  134.  
  135.         case srqGoAway:
  136.             result = handleGoAway(dataOut);
  137.             break;
  138.       }
  139.  
  140.     RestoreDB(oldB);
  141.     return( result ? 0x8000:0 );
  142. }
  143.  
  144.  
  145. unsigned handleHello()
  146. {
  147.     tellFinderAddToExtrasOut myResultRecord;
  148.  
  149.     /*
  150.        Menu item template--STATIC!  This must stay in memory until we remove
  151.        the item from the Extras menu.
  152.     */
  153.     static MenuItemTemplate myItemRecord = { 0x8000, 0, 0, 0, 0, 0,
  154.         (Ref) "\pFinderEx menu item" };
  155.  
  156.     SendRequest(tellFinderAddToExtras,sendToName+stopAfterOne,nameOfFinder,
  157.                 &myItemRecord,&myResultRecord);
  158.     myItemID = myResultRecord.menuItemID;
  159.     return(0x8000);
  160. }
  161.  
  162.  
  163. unsigned handleGoodbye()
  164. {
  165.     tellFinderRemoveFromExtrasOut myResultRecord;
  166.  
  167.     /*
  168.       If the finderSaysGoodbye request is redundant, just accept it
  169.       without doing anything.
  170.     */
  171.     if(!myItemID)
  172.         return(0x8000);
  173.  
  174.     /* Remove our menu item */
  175.     SendRequest(tellFinderRemoveFromExtras,sendToName+stopAfterOne,nameOfFinder,
  176.                 (long) myItemID, &myResultRecord);
  177.     myItemID = 0;
  178.     return(0x8000);
  179. }
  180.  
  181.  
  182. unsigned handleGoAway(dataOut)
  183.     srqGoAwayOutPtr dataOut;
  184. {
  185.  
  186.     /*
  187.        IMPORTANT -- Before accepting srqGoAway, disconnect your code
  188.        from the system (remove run queue tasks, heartbeat tasks, notify
  189.        procs, tool patches, interrupt handlers, etc.  The caller will
  190.        remove your request procedure (you can't safely remove a request
  191.        procedure from inside that procedure).  Then the caller will call
  192.        UserShutDown on you.
  193.  
  194.        If you can't remove all your hooks from the system, you should
  195.        accept srqGoAway but return resultID = $0000, telling the caller
  196.        you cannot go away.
  197.  
  198.        For Finder Extensions, you may assume that the caller already
  199.        sent you a finderSaysGoodbye message, if the Finder was active.
  200.     */
  201.  
  202.     dataOut->resultID = MMStartUp();    /* yes, we can go away */
  203.     dataOut->resultFlags = 0;           /* let's not restart from memory */
  204.     return(0x8000);
  205. }
  206.  
  207.  
  208. unsigned handleExtrasChosen(itemID)
  209.     unsigned itemID;
  210. {
  211.     char NumString[6];
  212.     char *subArray[2];
  213.     unsigned NumIcons;
  214.  
  215.     if(itemID != myItemID) return(0); /* not ours, so don't handle it */
  216.  
  217.     NumIcons = NumberOfIconsSelected();
  218.  
  219.     NumString[5] = '\0';
  220.     Int2Dec(NumIcons, NumString, 5, 0);
  221.     subArray[0] = NumString;
  222.     while(*subArray[0]==' ') { subArray[0]++; }; /* skip leading blanks */
  223.     subArray[1] = (NumIcons==1) ? "icon is" : "icons are";
  224.  
  225.     SysBeep2(sbOperationComplete+sbDefer);
  226.     AlertWindow(awButtonLayout+awCString,subArray,
  227.       "53/Yippety doo dah, it works!  By the way, *0 *1 selected./^#6");
  228.  
  229.     return(0x8000);
  230. }
  231.  
  232.  
  233. typedef unsigned **WordHandle;
  234.  
  235. unsigned NumberOfIconsSelected()
  236. {
  237.     unsigned num;
  238.     tellFinderGetSelectedIconsOut getIconsResult;
  239.     SendRequest(tellFinderGetSelectedIcons,sendToName+stopAfterOne,nameOfFinder,
  240.                 0L, &getIconsResult);
  241.     num = **((WordHandle) getIconsResult.stringListHandle);
  242.     DisposeHandle(getIconsResult.stringListHandle);
  243.     return(num);
  244. }
  245.